<?php

class account{
	
	var $usr;
	var $pwd;
	var $fullname;
	var $lastlog;
	
	// Class Constructor
	public function __construct(){
		$this->usr = '';
		$this->pwd = '';
	}
	
	public function set_user($user){
		$this->usr = $user;
	}
	
	public function set_password($password){
		$this->pwd = $password;
	}
	
	public function login(){
		$db = new DBClass();
		$query = "Select * from admin where username='$this->usr' and pass='$this->pwd'";
		$rows = $db->getRows($query);
				
		if($rows!=null){

			$this->fullname = $rows[0]['realname'];
			$this->lastlog = $rows[0]['lastlog'];
		
			$this->set_cookie();	
			$db->putRows("update admin set lastlog=now() where idadmin=".$rows[0]['idadmin']);
			return true;
		}else{
			return false;
		}
		
	}
	
	public function do_logout(){
		$this->delete_cookie();
	}
	
	protected function set_cookie(){
		setcookie("co_usr", $this->usr, time()+3600); 
		setcookie("co_pwd", $this->pwd, time()+3600); 
		setcookie("co_name", $this->fullname, time()+3600); 
		setcookie("co_log", $this->lastlog, time()+3600); 
	}
	
	public function get_cookie(){
		return $_COOKIE;
	}
	
	public function validate_cookie(){
		if($_COOKIE['co_usr'] == '' || $_COOKIE['co_pwd'] == '')
			return false;
		else return true;
	}

	protected function delete_cookie(){
		setcookie("co_usr", ''); 
		setcookie("co_pwd", ''); 
		setcookie("co_name", ''); 
		setcookie("co_log", ''); 
	}

	public function get_member(){
		$db = new DBClass();
		$sql = "SELECT * from member";
		$rows = $db->getRows($sql);
		return $rows;
	}

	public function del_member(){
		$db = new DBClass();
		$sql = "DELETE from member where idmember= '".$_GET['idmember']."'";
		$rows = $db->getRows($sql);
		return $rows;
	}

}